﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;

using CharityGamesOnline.Modules.Game;

namespace CharityGamesOnline.Modules.Game.Trivia
{
    /// <summary>
    /// Summary description for Trivia
    /// </summary>
    [WebService(Namespace = "http://www.charitygamesonline.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class Trivia : System.Web.Services.WebService, ICharityGameService
    {
        private const int SCORE_PER_QUESTION = 5;

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public int StartGame(int portalID, int enrollID, int userID)
        {
            int retInt = 0;
            TriviaController ctrlTrivia = new TriviaController();
            GameEnrollmentController ctrlEnroll = new GameEnrollmentController();
            GameEnrollmentInfo objEnroll = ctrlEnroll.GetEnrollment(portalID, enrollID);

            if (objEnroll != null && objEnroll.UserID == userID)
            {
                switch (objEnroll.EnrollStatusID)
                {
                    case (int)GameEnrollmentInfo.STATUS.PAID:
                        retInt = ctrlTrivia.QuestionInitialize(enrollID, userID);
                        break;
                    case (int)GameEnrollmentInfo.STATUS.PLAYING:
                        retInt = ctrlTrivia.QuestionRegenerate(enrollID, userID);
                        break;
                    case (int)GameEnrollmentInfo.STATUS.COMPLETED:
                        retInt = (int)GameEnrollmentInfo.ERRORCODE.NOTACTIVE;
                        break;
                    case (int)GameEnrollmentInfo.STATUS.ENROLLED:
                    default:
                        retInt = (int)GameEnrollmentInfo.ERRORCODE.NOTPAID;
                        break;
                }

                if (retInt > 0)
                {
                    if (objEnroll.EnrollStatusID == (int)GameEnrollmentInfo.STATUS.PAID)
                    {
                        objEnroll.EnrollStatusID = (int)GameEnrollmentInfo.STATUS.PLAYING;
                        objEnroll.ConnectCount = 1;
                    }
                    else
                    {
                        objEnroll.ConnectCount += 1;
                    }
                    ctrlEnroll.SaveEnrollment(objEnroll);
                }
            }
            else
                retInt = (int)GameEnrollmentInfo.ERRORCODE.NOTAUTHORIZED;

            return retInt; 
        }

        [WebMethod]
        public int EndGame(int portalID, int enrollID, int userID)
        {
            int retInt = 0;
            TriviaController ctrlTrivia = new TriviaController();
            GameEnrollmentController ctrlEnroll = new GameEnrollmentController();
            GameEnrollmentInfo objEnroll = ctrlEnroll.GetEnrollment(portalID, enrollID);

            if (objEnroll != null && objEnroll.UserID == userID)
            {
                switch (objEnroll.EnrollStatusID)
                {
                    case (int)GameEnrollmentInfo.STATUS.PLAYING:
                        
                        objEnroll.EndTime = DateTime.Now;
                        objEnroll.EnrollStatusID = (int)GameEnrollmentInfo.STATUS.COMPLETED;
                        ctrlEnroll.SaveEnrollment(objEnroll);
                        retInt = 1;
                        break;
                    case (int)GameEnrollmentInfo.STATUS.PAID:
                    case (int)GameEnrollmentInfo.STATUS.COMPLETED:
                    case (int)GameEnrollmentInfo.STATUS.ENROLLED:
                    default:
                        retInt = (int)GameEnrollmentInfo.ERRORCODE.NOTPLAYED;
                        break;
                }
            }
            else
                retInt = (int)GameEnrollmentInfo.ERRORCODE.NOTAUTHORIZED;

            return retInt;
        }

        [WebMethod]
        public decimal GetScore(int portalID, int enrollID, int userID)
        {
            decimal decScore = 0;
            GameEnrollmentController ctrlEnroll = new GameEnrollmentController();
            GameEnrollmentInfo objEnroll = ctrlEnroll.GetEnrollment(portalID, enrollID);

            if (objEnroll != null && objEnroll.UserID == userID)
            {
                decScore = objEnroll.Score;
            }
            else
                decScore = (int)GameEnrollmentInfo.ERRORCODE.NOTAUTHORIZED;

            return decScore;
        }

        [WebMethod]
        public int GetPlayedCount(int portalID, int enrollID)
        {
            int retInt = 0;
            GameEnrollmentController ctrlEnroll = new GameEnrollmentController();
            GameEnrollmentInfo objEnroll = ctrlEnroll.GetEnrollment(portalID, enrollID);
            if (objEnroll != null)
            {
                retInt = ctrlEnroll.GetPlayedCount(portalID, objEnroll.EventID, objEnroll.GameID, 0, (int)GameEnrollmentInfo.STATUS.COMPLETED);
            }
            return retInt;
        }

        [WebMethod]
        public int GetUserRank(int portalID, int enrollID, int userID)
        {
            int retInt = 0;
            GameEnrollmentController ctrlEnroll = new GameEnrollmentController();
            GameEnrollmentInfo objEnroll = ctrlEnroll.GetEnrollment(portalID, enrollID);
            if (objEnroll != null && objEnroll.UserID == userID)
            {
                retInt = ctrlEnroll.GetUserRank(enrollID, userID);
            }
            return retInt; 
        }

        [WebMethod]
        public TriviaQuestionInfo GetQuestion(int portalID, int enrollID,int userID)
        {
            TriviaQuestionInfo objQuestion = new TriviaQuestionInfo();
            TriviaController ctrlTrivia = new TriviaController();
            GameEnrollmentController ctrlEnroll = new GameEnrollmentController();
            GameEnrollmentInfo objEnroll = ctrlEnroll.GetEnrollment(portalID, enrollID);

            if (objEnroll != null && objEnroll.UserID == userID
             && objEnroll.EnrollStatusID == (int)GameEnrollmentInfo.STATUS.PLAYING)
            {
                objQuestion = ctrlTrivia.GetQuestion(enrollID, userID);
            }
            return objQuestion;
        }

        [WebMethod]
        public List<TriviaAnswerInfo> GetAnswers(int portalID, int enrollID, int userID, int questionIndex)
        {
            List<TriviaAnswerInfo> lstAnswer = new List<TriviaAnswerInfo>();
            TriviaController ctrlTrivia = new TriviaController();
            GameEnrollmentController ctrlEnroll = new GameEnrollmentController();
            GameEnrollmentInfo objEnroll = ctrlEnroll.GetEnrollment(portalID, enrollID);
            if (objEnroll != null && objEnroll.UserID == userID
             && objEnroll.EnrollStatusID == (int)GameEnrollmentInfo.STATUS.PLAYING)
            {
                lstAnswer = ctrlTrivia.GetAnswers(enrollID, userID, questionIndex);
            }

            if (lstAnswer.Count != 0)
                return lstAnswer;
            else
                return null;
        }

        [WebMethod]
        public TriviaResultSummaryInfo GetResultSummary(int portalID, int enrollID, int userID)
        {
            TriviaResultSummaryInfo objSummary = new TriviaResultSummaryInfo();
            TriviaController ctrlTrivia = new TriviaController();
            GameEnrollmentController ctrlEnroll = new GameEnrollmentController();
            GameEnrollmentInfo objEnroll = ctrlEnroll.GetEnrollment(portalID, enrollID);

            if (objEnroll != null && objEnroll.UserID == userID
             && objEnroll.EnrollStatusID == (int)GameEnrollmentInfo.STATUS.PLAYING)
            {
                objSummary = ctrlTrivia.GetResultSummary(enrollID, userID);
            }
            return objSummary;
        }


        [WebMethod]
        public int SendAnswer(int portalID, int enrollID, int userID, int questionIndex, int answerID, float gameDuration, int userScore)
        {
            int correctAnswerID = 0;
            TriviaController ctrlTrivia = new TriviaController();
            GameEnrollmentController ctrlEnroll = new GameEnrollmentController();
            GameEnrollmentInfo objEnroll = ctrlEnroll.GetEnrollment(portalID, enrollID);
            if (objEnroll != null && objEnroll.UserID == userID
             && objEnroll.EnrollStatusID == (int)GameEnrollmentInfo.STATUS.PLAYING)
            {
                correctAnswerID = ctrlTrivia.QuestionAnswer(enrollID, userID, questionIndex, answerID, gameDuration, userScore);
            }

            return correctAnswerID;
        }        

        private int GetScore(TriviaResultSummaryInfo e)
        {
            int intScore = 0;
            if (e != null)
            {
                intScore = SCORE_PER_QUESTION * e.CorrectCount;
            }
            return intScore;
        }
    }
}
